package ru.timofeyco.justfeelbetter;

import java.io.IOException;

import ru.timofeyco.justfeelbetter.db.JustFeelBetterDatabase;
import ru.timofeyco.justfeelbetter.model.DrillType;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class AddDrillTypeActivity extends Activity {
	private JustFeelBetterDatabase dbHelper;
	
	private EditText nameEditText;
	private EditText rateEditText;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_drill_type);
        
        this.dbHelper = new JustFeelBetterDatabase(this);
        nameEditText = (EditText) findViewById(R.id.drill_type_name_edit);
        rateEditText = (EditText) findViewById(R.id.drill_type_rate_edit);
	}
	
	public void onAddDrillType(View v) {
		String name = nameEditText.getText().toString();
		String rateStr = rateEditText.getText().toString();
		int rate = (rateStr.length() == 0) ? 10 : Integer.valueOf(rateStr);
		
		DrillType newDrillType = new DrillType(name, rate, Color.BLACK);
		try {
			dbHelper.insertDrillType(newDrillType);
		} catch (IOException e) {
			Toast.makeText(this, getString(R.string.drill_type_already_exists), Toast.LENGTH_LONG).show();
		}
		
		this.finishActivity(RESULT_OK);
		this.finish();		
	}
	
	public void onCancelDrillType(View v) {
		this.finishActivity(RESULT_CANCELED);
		this.finish();
	}
}
